package com.weiaibenpao.demo.chislim.ui.activity.running;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.model.LatLng;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.ui.adapter.SearchLocationAdapter;
import com.weiaibenpao.demo.chislim.utils.LoadingDialogUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchLocationActivity extends BaseActivity implements PoiSearch.OnPoiSearchListener,BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.img_search)
    ImageView imgSearch;
    @BindView(R.id.recycler_location)
    RecyclerView recyclerLocation;
    @BindView(R.id.et_location)
    EditText etLocation;
    @BindView(R.id.text_search)
    TextView textSearch;
    @BindView(R.id.img_clear)
    ImageView imgClear;
    private PoiSearch poiSearch;
    private List<PoiItem> poiItems;

    private int currentPage=1;
    private SearchLocationAdapter locationAdapter;
    private LoadingDialogUtils.Builder dialog;
    //    private InputMethodManager imm;

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_search_location;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        poiItems = new ArrayList<>();
        dialog = new LoadingDialogUtils.Builder(this).setTitle("正在加载...").setCancelable(true);

        initAdapter();
        etLocation.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                if (charSequence.length() > 0) {
                    imgSearch.setVisibility(View.GONE);
                    imgClear.setVisibility(View.VISIBLE);
                    textSearch.setVisibility(View.VISIBLE);
                } else if (charSequence.length() <= 0) {
                    imgSearch.setVisibility(View.VISIBLE);
                    imgClear.setVisibility(View.GONE);
                    textSearch.setVisibility(View.GONE);
                }
                String newText = etLocation.getText().toString().trim();
                Logger.e("newText==="+newText);
                if (!TextUtils.isEmpty(newText))
                {
                    currentPage=1;
                    poiSearch(newText);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                    if (editable.length()<=0){
                        imgSearch.setVisibility(View.VISIBLE);
                        imgClear.setVisibility(View.GONE);
                        textSearch.setVisibility(View.GONE);
                    }
            }
        });

    }

    private void initAdapter() {
        locationAdapter = new SearchLocationAdapter(R.layout.location_item);
        locationAdapter.setOnItemClickListener(this);

        recyclerLocation.setLayoutManager(new LinearLayoutManager(this));
        recyclerLocation.setAdapter(locationAdapter);

    }

    public static void openActivity(Context context) {
        Intent intent = new Intent(context, SearchLocationActivity.class);
        context.startActivity(intent);
    }

    @OnClick({R.id.img_back, R.id.text_search, R.id.img_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.text_search:
                currentPage++;
                poiSearch(etLocation.getText().toString().trim());

                break;
            case R.id.img_clear:
                etLocation.setText("");

                break;
        }
    }


    private void poiSearch(String code) {
        dialog.show();
        locationAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT );

        PoiSearch.Query query = new PoiSearch.Query(code, "", "");
        query.setPageSize(15);

        query.setPageNum(currentPage);
        poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(this);
        //调用 PoiSearch 的 searchPOIAsyn() 方法发送请求。
        poiSearch.searchPOIAsyn();
    }


    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        dialog.dismiss();
        if (i == 1000) {
            if (poiResult != null && poiResult.getPois() != null && poiResult.getPois().size() != 0) {
                poiItems = poiResult.getPois();
                locationAdapter.setNewData(poiItems);
            }
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        LatLonPoint latLonPoint = locationAdapter.getItem(position).getLatLonPoint();
        EventBus.getDefault().post(new LatLng(latLonPoint.getLatitude(),latLonPoint.getLongitude()));
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (dialog!=null){
            dialog.dismiss();
        }
    }
}
